/*
 * @Description:
 * @Author: wsm
 * @Date: 2021-07-15 09:42:57
 * @LastEditTime : 2022-02-25 15:34:50
 * @LastEditors  : Shiming
 * @Reference:
 */
import { AfterViewInit, Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { STChange, STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFRadioWidgetSchema, SFSchema, SFTextWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { NzModalService } from 'ng-zorro-antd/modal';
import { filter } from 'rxjs/operators';


@Component({
  selector: 'app-supply-goods-components-list-carload',
  templateUrl: './carload.component.html',
  styleUrls: ['./carload.component.less'],
})
export class SupplyGoodsComponentListCarloadComponent implements OnInit, AfterViewInit {
  @ViewChild('st', { static: true }) st!: STComponent;
  @ViewChild('stAssign', { static: true }) stAssign!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @ViewChild('sfAssign', { static: false }) sfAssign!: SFComponent;
  @ViewChild('sfFre', { static: false }) sfFre!: SFComponent;
  isVisible = false;
  isVisibleAssign = false;
  ColumnsAssign!: STColumn[];
  ui: SFUISchema = {};
  uiFre: SFUISchema = {};
  mode: any = 'search';
  demoValue: any;
  tabs = ['全部', '待接单',  '已接单',  '已取消'];
  listData: any =[
    {
      id: 1,
      id2: 2,
      id3:3,
      id4: 4,
    }
  ]
  listData2: any = [
      {
        clientGroupName: '姓名',
        clientCount: '姓名',
        clientCount2: '姓名',
        clientCount3: '姓名',
      }
    ]
  orderStatus: any = '0'; // 订单状态, 10为已提交待付款, 20为已付款待发,30为已发货待收货,50取消订单,40 已完成
  nzSelectedIndex = 0;
  total = 1;
  loading = false;
  pageSize = 20;
  pageIndex = 1;
  toSendCount = 0;
  addTimeStart = '';
  addTimeEnd = '';
  sfExpand = false;
  _$expand = false;
  ssdsad: any;
  schema!: SFSchema;
  schemaAssign!: SFSchema;
  freightSchema!: SFSchema;
  payStatus: any;
  constructor(
    private fb: FormBuilder,
    private modal: NzModalService,
    private router: Router,
    private ar: ActivatedRoute,
    private modalHelper: ModalHelper,
  ) {}

  ngOnInit(): void {
    this.getList();
    this.initSF();
    this.initUI();
    this.initSFFre();
    this.initSFAssign();
  }

  ngAfterViewInit() {
    // this.router.events.pipe(filter((evt) => evt instanceof NavigationEnd)).subscribe(() => {
    //
    // });
  }
  /**
   * 指定客户分组查询参数
   */
   get reqParamsAssign() {
    const params = Object.assign({});
    return { ...params, ...this.sfAssign?.value };
  }
  change(change: STChange) {
    console.log(change.checkbox)
  }
  initSFFre() {
    this.freightSchema = {
      properties: {
        btn: {
          type: 'string',
          title: '是否回单',
          enum: ['需要', '不需要'],
          ui: {
            widget: 'radio',
          } as SFRadioWidgetSchema,
          default: '需要',
        },
        contactsName: {
          title: '预付',
          type: 'string',
          minimum:0,
          maximum:99999,
          ui: {
            showRequired: false,
          },
        },
        unifiedSocialCreditCode: {
          title: '到付',
          type: 'string',
          minimum:0,
          maximum:99999,
          ui: {
            showRequired: false,
          },
        },
        // unifiedSocialCreditCode2: {
        //   title: '油卡',
        //   type: 'string',
        //   minimum:0,
        //   maximum:99999,
        //   ui: {
        //     showRequired: false,
        //   },
        // },
        unifiedSocialCreditCode3: {
          title: '回单付',
          type: 'string',
          minimum:0,
          maximum:99999,
          ui: {
            showRequired: false,
          },
        },
        '小计': { type: 'number', ui: { widget: 'text', defaultText: '5000 text' } as SFTextWidgetSchema },
        fujia: { type: 'string',title: '附加费',  ui: { widget: 'custom'}},
        manys: { type: 'string',title: '总费用',  ui: { widget: 'custom'}},
        unifiedSocia: { type: 'string',title: '总费用',  ui: { widget: 'custom'}},
 
      },
    };
    this.schemaAssign = {
      properties: {
        clientGroupName: {
          title: '分组名称',
          type: 'string',
          ui: {
            placeholder: '请输入司机姓名/手机号',
            enter: (e: KeyboardEvent) => {
              this.stAssign.load();
            },
          },
        },
        clientGroupName2: {
          title: '分组名称',
          type: 'string',
          ui: {
            placeholder: '请输入车牌号',
            enter: (e: KeyboardEvent) => {
              this.stAssign.load();
            },
          },
        },
      },
    };
    this.uiFre = { '*': { spanLabelFixed: 90, grid: { span: 16 }, enter: () => this.st.load() } };
  }
  initSFAssign() {
    this.ColumnsAssign = [
      { title: '', type: 'checkbox', width: '40px', className: 'text-center' },
      { title: '司机姓名', index: 'clientGroupName', width: '300px', className: 'text-center' },
      { title: '手机号', index: 'clientCount', width: '300px', className: 'text-center' },
      { title: '车牌号', index: 'clientCount2', width: '300px', className: 'text-center' },
      { title: '状态', index: 'clientCount3', width: '300px', className: 'text-center' },
    ];
  }
  
  initSF() {
    this.schema = {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true,
          },
        },
        storeName: { title: '货源编号', type: 'string', ui: { showRequired: false } },
        contactsName: {
          title: '货物名称',
          type: 'string',
          ui: {
            showRequired: false,
          },
        },
        unifiedSocialCreditCode: {
          title: '装货地',
          type: 'string',
          ui: {
            showRequired: false,
          },
        },
        unifiedSocialCreditCode2: {
          title: '卸货地',
          type: 'string',
          ui: {
            showRequired: false,
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        unifiedSocialCreditCode3: {
          title: '货主',
          type: 'string',
          ui: {
            showRequired: false,
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        enStatus2: {
          type: 'string',
          title: '审核状态',
          enum: [
            { label: '全部', value: '' },
            { label: '正常', value: 0 },
            { label: '冻结', value: 1 },
            { label: '废弃', value: 2 },
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
      },
    };

    this.ui = { '*': { spanLabelFixed: 90, grid: { span: 8, gutter: 4 }, enter: () => this.st.load() } };
  }
  initUI() {
    this.ui = {
      '*': {
        spanLabelFixed: 90,
        grid: { span: 8 },
      },
    };
  }
  tabChange(i: number) {
    console.log(i)
    this.changeIndex(i);
    this.getList();
  }
  changeIndex(i: number) {
    if (i === 0) {
      this.orderStatus = '0';
      this.payStatus = null;
    } else if (i === 1) {
      this.orderStatus = '5';
      this.payStatus = null;
    } else if (i === 2) {
      this.orderStatus = null;
      this.payStatus = '10';
    } else if (i === 3) {
      this.orderStatus = '20';
      this.payStatus = null;
    }
    this.pageIndex = 1;
  }
  getList(type?: string) {
    // this.loading = true;
    const params: any = {
      pageSize: this.pageSize,
      pageIndex: this.pageIndex,
      ...this.sf?.value,
      orderStatus: this.orderStatus,
      payStatus: this.payStatus,
      createTime: this.sf?.value.createTime || [],
      time: {},
    };
    if (this.sf?.value?.createTime) {
      delete params.createTime;
      params.time.start = this.sf?.value?.createTime[0];
      params.time.end = this.sf?.value?.createTime[1];
    }
    delete params.expand;
    if (type === 'search') {
      params.pageIndex = 1;
    }
    // this.service.request(this.service.$api_get_page, params).subscribe((res) => {
    //   this.loading = false;
    //   this.listData = res.orderPage.records;
    //   this.toSendCount = res.toSendCount;
    //   this.listData.forEach((element: { goodsList: { goodsSpecJson: string }[] }) => {
    //     element.goodsList.forEach((item: { goodsSpecJson: string }) => {
    //       if (item.goodsSpecJson) {
    //         item.goodsSpecJson = JSON.parse(item.goodsSpecJson);
    //       }
    //     });
    //   });
    //   this.total = res.orderPage.total;
    // });
  }

  orderExport() {
    // this.service.exportStart(params, this.service.$api_export);
    this.modal.create({
      nzTitle: '订单导出',
      // nzContent: this.exportTemplate,
      // nzContent: `待导出订单总数：${this.pageInfo.total}条，确定要导出吗？`,
      nzOnOk: () => {
        const params: any = {
          pageSize: this.pageSize,
          pageIndex: this.pageIndex,
          ...this.sf?.value,
          orderStatus: this.orderStatus,
          // createTime: this.sf?.value.createTime || [],
        };
        delete params.expand;
      },
    });
  }

  goDetail(data: any) {
    this.router.navigate(['../order-detail', data.orderSn], { relativeTo: this.ar });
  }


  changePageIndex(pageIndex: number) {
    this.pageIndex = pageIndex;
    this.getList();
  }
  changePageSize(value: number) {
    this.pageSize = value;
    this.getList();
  }
  expandToggle() {
    this.sfExpand = !this.sfExpand;
    this.sf?.setValue('/expand', this.sfExpand);
  }

  showApply() {
    this.isVisible = true
  }
  showAssign() {
    this.isVisibleAssign = true
  }
  handleOK() {

  }
handleCancel(type: any) {
  if(type === 'suppliersType') {
    this.isVisible = false
  } else if(type === 'assign') {
    this.isVisibleAssign = false
  }
}
  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this.sfExpand = false;
  }
  resetSFAssign() {
    this.sfAssign.reset();
  }



   /**
   * 查询字段个数navigate
   */
    get queryFieldCount(): number {
      return Object.keys(this.schema?.properties || {}).length;
    }
}
